<?php
namespace org\phpstruct\db;

use org\phpstruct\db\I;
use org\phpstruct\db\driver;

class DBFactory extends \PDO {
  const DRIVER_OCI = 1,
  DRIVER_PDO_ORACLE = 2,
  DRIVER_PDO_MYSQL = 3;

  private static $instances = array();

  /**
   * Enter description here...
   *
   * @param int $driver
   * @param string $dsn
   * @param string $username
   * @param string $password
   * @return I\DBDriverInterface
   */
  public static function getConnection($driver=self::DRIVER_PDO_MYSQL, $dsn=null, $username=null, $password=null) {

	switch ($driver) {
	  case self::DRIVER_OCI:
		$idconn = md5($dsn . $username . $password);

		if (isset(self::$instances[self::DRIVER_OCI][$idconn]) && (self::$instances[self::DRIVER_OCI][$idconn])) {
		  return self::$instances[self::DRIVER_OCI][$idconn];
		} else {
		  $dbh = self::$instances[self::DRIVER_OCI][$idconn] = new driver\OCI($dsn, $username, $password);
		  return $dbh;
		}
		break;
	  case self::DRIVER_PDO_ORACLE:
		throw new Exception(_("NOT YET IMPLEMENTED"));
		break;
	  case self::DRIVER_PDO_MYSQL:
		$idconn = md5($dsn . $username . $password);

		if (isset(self::$instances[self::DRIVER_PDO_MYSQL][$idconn]) && (self::$instances[self::DRIVER_PDO_MYSQL][$idconn])) {
		  return self::$instances[self::DRIVER_PDO_MYSQL][$idconn];
		} else {
		  $dbh = self::$instances[self::DRIVER_PDO_MYSQL][$idconn] = new driver\MySQL($dsn, $username, $password);
		  return $dbh;
		}
		break;
	  default:
		throw new Exception(_("NOT YET IMPLEMENTED"));
		break;
	}
  }

}
